﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Specialized;
using System.Windows.Input;
using System.Windows.Controls.Primitives;

namespace ZOIL.Library.Components
{
    public class VHyperGrid2b : DataGrid
    {
        static VHyperGrid2b()
        {
            #region set default values for hypergrid (the ones which differ from datagrid)
		    Type ownerType = typeof(VHyperGrid2b);

            //auto generate columns
            AutoGenerateColumnsProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(false));
            //TODO setting columnwidth is not working...
            ColumnWidthProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(new DataGridLength(1,DataGridLengthUnitType.Star)));

            //scrollbars
            HorizontalScrollBarVisibilityProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ScrollBarVisibility.Hidden));
            VerticalScrollBarVisibilityProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ScrollBarVisibility.Visible));
            
            //gridlines
            GridLinesVisibilityProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(DataGridGridLinesVisibility.Horizontal));

            //columns
            HeadersVisibilityProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(DataGridHeadersVisibility.All));
            CanUserSortColumnsProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));
            CanUserReorderColumnsProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));
            CanUserResizeColumnsProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));
            ColumnHeaderHeightProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(50.0));
            EnableColumnVirtualizationProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));

            //rows
            RowHeightProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(20.0));
            MinRowHeightProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata((double)5.0));
            CanUserAddRowsProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(false));
            CanUserResizeRowsProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));
            EnableRowVirtualizationProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));

            //alternating row background
            AlternationCountProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(2));
            AlternatingRowBackgroundProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(Brushes.Gray));
            
            //selection mode
            SelectionModeProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(DataGridSelectionMode.Extended));
	        #endregion
        }

        public VHyperGrid2b()
        {
            this.Loaded += new RoutedEventHandler(VHyperGrid2b_Loaded);
        }

        void VHyperGrid2b_Loaded(object sender, RoutedEventArgs e)
        {
            //refresh Properties which aren't set correctly
            refreshProperties();

            //set width of all columns to auto
            //for (int i = 0; i < this.Columns.Count(); i++)
            //{
            //    DataGridColumn currDGC = (DataGridColumn)this.Columns[i];
            //    currDGC.Width = new DataGridLength(10, DataGridLengthUnitType.Star);
            //    currDGC.IsReadOnly = true; 
            //    //TODO: set sortmemberpath to top-most attribute
            //    //currDGC.SortMemberPath = attributesInColumns[i][0];
            //}

            //set styles, templates & events
            setTemplates();
        }

        //TODO probably on "Initialized" or "OnApplyTemplate"
        private void setTemplates()
        {
            //set styles

            //FrameworkElement tempStyleFE = new VHyperGrid2Styles();
            //Style HGColumnHeaderStyle = (Style)tempStyleFE.FindResource("HGColumnHeaderStyle");
            //ColumnHeaderStyle = HGColumnHeaderStyle;

            Style HGColumnHeaderStyle = new Style(typeof(DataGridColumnHeader));
            HGColumnHeaderStyle.Setters.Add(new Setter(SnapsToDevicePixelsProperty, true));
            HGColumnHeaderStyle.Setters.Add(new Setter(BackgroundProperty, Brushes.Brown));
            HGColumnHeaderStyle.Setters.Add(new Setter(CursorProperty, Cursors.Hand));
            //HGColumnHeaderStyle.Setters.Add(new Setter(SnapsToDevicePixelsProperty, true));

            ColumnHeaderStyle = HGColumnHeaderStyle;


            //VHyperGrid2b.ColumnHeaderStyleProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(null));
            //ColumnReordered="HyperGrid_ColumnReordered"
            //Template="{StaticResource HyperGridTemplate}"
            //RowHeaderTemplate="{DynamicResource RowHeaderTemplate}"
        }

        private void refreshProperties()
        {
            //dirty hack because ...Property.OverrideMetadata is set in Backend but not refreshed on UserInterface
            this.AlternatingRowBackground = this.AlternatingRowBackground;
            this.RowHeight = this.RowHeight;
            this.MinRowHeight = this.MinRowHeight;
            this.ColumnHeaderHeight = this.ColumnHeaderHeight;
            this.ColumnWidth = this.ColumnWidth;
        }
    }
}
